// ReportTempletUpdateHandler.cpp: implementation of the ReportTempletUpdateHandler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "kwservercore.h"
#include "ReportTempletUpdateHandler.h"
#include "DatabaseManager.h"
#include "ReportTempletSet.h"
#include "ReportTempletQueryHandler.h"
#include "TimeManager.h"

#include "CommonLib\Structure.h"
#include "CommonLib\Packet.h"

#include "CommServerLib\CommWithClient.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

ReportTempletUpdateHandler::ReportTempletUpdateHandler(const Packet& packet, HANDLE hClient):
	BaseRequestHandler(packet, hClient)
{
}

ReportTempletUpdateHandler::~ReportTempletUpdateHandler()
{
}

int ReportTempletUpdateHandler::Handling()
{
	ReportTempletUpdateStruct * pReportTempletUpdateStruct = (ReportTempletUpdateStruct *)m_pPacket->GetpData();

	int nMethodAck;
	switch (pReportTempletUpdateStruct->m_dwMethodType)
	{
	case VCS_MT_ADDREPORTTEMPLET:
		nMethodAck = AddReportTemplet(pReportTempletUpdateStruct->m_ReportTempletItem);
		break;
	case VCS_MT_MODIFYREPORTTEMPLET:
		nMethodAck = ModifyReportTemplet(pReportTempletUpdateStruct->m_ReportTempletItem);
		break;
	case VCS_MT_DELETEREPORTTEMPLET:
		nMethodAck = DeleteReportTemplet(pReportTempletUpdateStruct->m_ReportTempletItem);
		break;
	case VCS_MT_RENAMEREPORTTEMPLET:
		nMethodAck = RenameReportTemplet(pReportTempletUpdateStruct->m_ReportTempletItem);
		break;
	default:
		ASSERT(false);
		nMethodAck = VCS_MR_UNKNOWNMETHOD;
		break;
	}

	ReportTempletAckStruct * pReportTempletAckStruct = ReportTempletQueryHandler::QueryReportTemplet(pReportTempletUpdateStruct->m_ReportTempletItem.m_szUserId);
	pReportTempletAckStruct->m_dwMethodType = pReportTempletUpdateStruct->m_dwMethodType;
	pReportTempletAckStruct->m_nMethodAck = nMethodAck;

	Packet packetAck(pReportTempletAckStruct->m_dwBufSize, (LPBYTE)pReportTempletAckStruct);
	packetAck.m_dwId = m_pPacket->m_dwId;
	CommWithClient::Instance()->SendTo(packetAck, m_hClient);

	delete pReportTempletAckStruct;

	return 0;
}

int ReportTempletUpdateHandler::AddReportTemplet(const ReportTempletItem& rti)
{
	int nRet = VCS_MR_OK;

	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CReportTempletSet ReportTempletSet(pDatabase);

	ReportTempletSet.m_strFilter.Format("UserId = '%s' AND TempletName = '%s'", rti.m_szUserId, rti.m_szTempletName);
	ReportTempletSet.Open(AFX_DB_USE_DEFAULT_TYPE, NULL);
	if (!ReportTempletSet.IsEOF())
	{
		nRet = VCS_MR_TEMPLETEXIST;
	}
	else
	{
		ReportTempletSet.AddNew();
		ReportTempletSet.m_UserId = rti.m_szUserId;
		ReportTempletSet.m_TempletName = rti.m_szTempletName;
		const CTime& time = TimeManager::Instance()->GetTime();
		ReportTempletSet.m_CreateDate = time.Format("%Y-%m-%d");
		ReportTempletSet.m_CreateTime = time.Format("%H:%M:%S");
		ReportTempletSet.m_ShareFlag = rti.m_nShareFlag;
		ReportTempletSet.m_Data.SetSize(rti.m_dwDataSize+1);
		for (int i = 0;i < rti.m_dwDataSize;i++)
		{
			ReportTempletSet.m_Data.SetAt(i, rti.m_byData[i]);
		}
		ReportTempletSet.m_Data.SetAt(rti.m_dwDataSize, 0xff);
		ReportTempletSet.Update();
	}
	ReportTempletSet.Close();

	return nRet;
}

int ReportTempletUpdateHandler::ModifyReportTemplet(const ReportTempletItem& rti)
{
	int nRet = VCS_MR_OK;

	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CReportTempletSet ReportTempletSet(pDatabase);

	ReportTempletSet.m_strFilter.Format("UserId = '%s' AND TempletName = '%s'", rti.m_szUserId, rti.m_szTempletName);
	ReportTempletSet.Open(AFX_DB_USE_DEFAULT_TYPE, NULL);
	if (ReportTempletSet.IsEOF())
	{
		nRet = VCS_MR_TEMPLETNOTEXIST;
	}
	else
	{
		try{
			ReportTempletSet.Edit();
			const CTime& time = TimeManager::Instance()->GetTime();
			ReportTempletSet.m_CreateDate = time.Format("%Y-%m-%d");
			ReportTempletSet.m_CreateTime = time.Format("%H:%M:%S");
			ReportTempletSet.m_ShareFlag = rti.m_nShareFlag;
			ReportTempletSet.m_Data.SetSize(rti.m_dwDataSize+1);
			for (int i = 0;i < rti.m_dwDataSize;i++)
			{
				ReportTempletSet.m_Data.SetAt(i, rti.m_byData[i]);
			}
			ReportTempletSet.m_Data.SetAt(rti.m_dwDataSize, 0xff);
			ReportTempletSet.Update();
		}
		catch(...)
		{
		}
	}
	ReportTempletSet.Close();

	return nRet;
}

int ReportTempletUpdateHandler::DeleteReportTemplet(const ReportTempletItem& rti)
{
	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();

	CString strSql;
	strSql.Format("DELETE FROM ReportTemplet WHERE UserId = '%s' AND TempletName = '%s'", rti.m_szUserId, rti.m_szTempletName);
	pDatabase->ExecuteSQL(strSql);

	return VCS_MR_OK;
}

int ReportTempletUpdateHandler::RenameReportTemplet(const ReportTempletItem& rti)
{
	int nRet = VCS_MR_OK;

	CDatabase * pDatabase = DatabaseManager::Instance()->GetpDatabase();
	CReportTempletSet ReportTempletSet(pDatabase);

	if (stricmp((LPCTSTR)rti.m_byData, rti.m_szTempletName) != 0)
	{
		ReportTempletSet.m_strFilter.Format("UserId = '%s' AND TempletName = '%s'", rti.m_szUserId, (LPCTSTR)rti.m_byData);
		ReportTempletSet.Open(AFX_DB_USE_DEFAULT_TYPE, NULL);
		if (!ReportTempletSet.IsEOF())
		{
			ReportTempletSet.Close();
			nRet = VCS_MR_TEMPLETEXIST;
		}
		else
		{
			ReportTempletSet.Close();
			CString strSql;
			strSql.Format("UPDATE ReportTemplet SET TempletName = '%s' WHERE UserId = '%s' AND TempletName = '%s'", (LPCTSTR)rti.m_byData, rti.m_szUserId, rti.m_szTempletName);
			pDatabase->ExecuteSQL(strSql);
		}
	}
	else
	{
		CString strSql;
		strSql.Format("UPDATE ReportTemplet SET TempletName = '%s' WHERE UserId = '%s' AND TempletName = '%s'", (LPCTSTR)rti.m_byData, rti.m_szUserId, rti.m_szTempletName);
		pDatabase->ExecuteSQL(strSql);
	}

	return nRet;
}